package middleware

import (
	"github.com/gin-gonic/gin"
)

// MidURLAccess is url control visit
func MidURLAccess(c *gin.Context) {
	lgusr := getThisUser(c)
	if lgusr == "" {
		c.String(403, "not login")
		c.Abort()
		return
	}
	c.Set("lguser", lgusr)
}

// getThisUser get current visit user
func getThisUser(c *gin.Context) string {
	token := c.Request.Header.Get("Token")
	if token == "" {
		cki, err := c.Request.Cookie("Token")
		if err == nil {
			token = cki.Value
		}
	}
	return token
}
